package CollectionsDay02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ListDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("five");
        System.out.println("list:"+list.toString());

        /*
            E get(int index):
            获取制定下标所对应的元素
         */
        String e = list.get(2);
        System.out.println(e);//three

        System.out.println("----------------------------------");
        for (int i =0; i<list.size();i++){
            System.out.println(list.get(i));
        }
        for (String s : list){
            System.out.println(s);
        }
        Iterator<String> it = list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }

        System.out.println("----------------------------------");

        /*
        E set(int index,E e)
        将指定元素设置到指定位置,返回被替代的元素
         */
        String old = list.set(2,"six");//将list中下标为2的元素设置为six
//        list.set(2,"six");//常规用法
        System.out.println(list);//[one, two, six, four, five]
        System.out.println(old);

        /*
        java.util.Collections为集合的工具类,包含了集合相关的静态方法
         */
        Collections.reverse(list);//反转list集合元素(下标变化了)
        System.out.println(list);//[five, four, six, two, one]

        /*
            void add(int index,E e)
            将给定元素e添加到index所指定的位置,相当于插入操作
         */
        list.add(2,"three");
        System.out.println(list);//[five, four, three, six, two, one]

        /*
            E remove(int index)
            删除指定位置元素,并返回指定位置元素
         */
        String o = list.remove(2);
//        list.remove(2);常规操作
        System.out.println(list);//[five, four, six, two, one]
        System.out.println(o);//three
    }
}
